package com.app.backend.base.serviceImpl;

import com.app.backend.base.dao.UserDao;
import com.app.backend.base.dao.UserRoleDao;
import com.app.backend.base.entity.User;
import com.app.backend.base.entity.UserRole;
import com.app.backend.base.service.UserRoleService;
import com.app.backend.common.base.BaseDao;
import com.app.backend.common.constant.CommonConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: UserRoleServiceImpl
 * @description:
 * @author: Zhun.Xiao
 * @create: 2020-03-03 09:13
 **/



@Service
@Transactional(rollbackFor = Exception.class)
public class UserRoleServiceImpl implements UserRoleService {

    @Autowired
    UserRoleDao userRoleDao;


    @Autowired
    UserDao userDao;
    @Override
    public BaseDao<UserRole, String> getRepository() {
        return userRoleDao;
    }

    @Override
    public List<User> findUserByRoleId(String id) {
        List<UserRole> list = userRoleDao.findByRoleId(id);
        List<User> users = new ArrayList<>();
        for (UserRole userRole : list) {
            User user = userDao.findById(userRole.getUserId()).orElse(null);
            if (null != user && CommonConstant.USER_STATUS_NORMAL.equals(user.getStatus())) {
                users.add(user);
            }
        }
        return users;
    }
}